package cn.fl.institution.controller;

import cmm.mid.core.framework.dto.DataResultDTO;
import cmm.mid.core.framework.page.DataResultVO;
import cn.cmm.base.framework.constant.BaseConstant;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.DecimalUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.fl.customer.service.ICstBscInfoService;
import cn.fl.customer.service.ICstNpBscInfoService;
import cn.fl.customer.vo.CstBscInfoVO;
import cn.fl.customer.vo.CstNpBscInfoVO;
import cn.fl.framework.constant.DictionaryConstant;
import cn.fl.project.api.IPrjCstAccInfoApiService;
import cn.fl.project.condition.PrjCstAccInfoQueryCondition;
import cn.fl.project.constant.PrjBscInfoConstant;
import cn.fl.project.service.IPrjBscInfoService;
import cn.fl.project.vo.PrjBscInfoVO;
import cn.fl.project.vo.PrjCstAccInfoVO;
import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * @Description: 项目基础信息表 控制层 浙银金租
 */
@Controller
@RequestMapping("/institution/zyfl")
public class PrjBscInfoInsZYFLController {

	private static final Logger logger = LoggerFactory.getLogger(PrjBscInfoInsZYFLController.class);

    @Autowired
    private IPrjBscInfoService prjBscInfoService; // 项目基础信息表

	@Autowired
	private IPrjCstAccInfoApiService prjCstAccInfoApiService; // 项目客户账户信息表
	
	@Autowired
	private ICstBscInfoService cstBscInfoService; // 客户基础信息
	
	@Autowired
	private ICstNpBscInfoService cstNpBscInfoService; // 自然人信息
	
	/**
	 * 保存 项目客户信息。
	 */
	@RequestMapping(value = "/save.do", method = { RequestMethod.POST })
	@ResponseBody
	public Object save(Long prjId, HttpServletRequest request, HttpServletResponse response) throws BaseException {
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, "四要素校验通过");
//
		try {
			PrjBscInfoVO prjBscInfoVO = this.prjBscInfoService.selectById(prjId);
			BaseLogUtils.info(logger, "save", "浙银金租立项保存 项目基本信息： prjBscInfoVO = " + JSONObject.toJSONString(prjBscInfoVO));

			PrjCstAccInfoVO prjCstAccInfoVO = prjBscInfoVO.getPrjCstAccInfoVO();
			BaseLogUtils.info(logger, "save", "浙银金租立项保存 原项目客户信息： prjCstAccInfoVO = " + JSONObject.toJSONString(prjCstAccInfoVO));

			CstBscInfoVO cstBscInfoVO = this.cstBscInfoService.selectDetailById(prjBscInfoVO.getCstId());
			PrjCstAccInfoQueryCondition prjCstAccInfoQC = this.getRequestParamForPrjCstAccInfo(request, prjCstAccInfoVO);

			// 修改自然人信息：证件有效起始日期、证件有效终止日期
			CstNpBscInfoVO cstNpBscInfoVO = cstBscInfoVO == null ? null : cstBscInfoVO.getCstNpBscInfoVO();
			if(null != cstNpBscInfoVO && StringUtils.isNotEmpty(cstNpBscInfoVO.getId())) {
				CstNpBscInfoVO modifiedCstNpBscInfoVO = new CstNpBscInfoVO();
				modifiedCstNpBscInfoVO.setId(cstNpBscInfoVO.getId());
				this.getRequestParamForCstNpBscInfo(request, modifiedCstNpBscInfoVO);
				BaseLogUtils.info(logger, "save", "浙银金租立项保存 页面提交的自然人信息： " + JSONObject.toJSONString(modifiedCstNpBscInfoVO));
				cstNpBscInfoService.modify(modifiedCstNpBscInfoVO);
			}
			else {
				BaseLogUtils.info(logger, "save", "浙银金租立项保存 自然人信息不存在");
				result.setSuccess(BaseConstant.IS_YESNO_NO);
				result.setInfo("自然人信息不存在!");
				return result;
			}

			// 保存项目客户信息，包含四要素验证
            prjCstAccInfoQC.setInsCode(PrjBscInfoConstant.PRJ_INS_CODE_ZYFL);
			BaseLogUtils.info(logger, "save", "浙银金租立项保存 页面提交的项目客户信息： " + JSONObject.toJSONString(prjCstAccInfoQC));
			DataResultDTO resultDTO = this.prjCstAccInfoApiService.modifyPrjAccInfo(prjCstAccInfoQC);

			// 判断四要素校验结果
			BaseLogUtils.info(logger, "save", "浙银金租立项保存 四要素验证结果 ： " + JSONObject.toJSONString(resultDTO));
			String chkRstCd = null, rstDesc = null;
			if(resultDTO != null && StringUtils.isNotEmpty(resultDTO.getCode())
					&& Integer.valueOf(resultDTO.getCode()) != BaseConstant.IS_YESNO_YES) {
				chkRstCd = resultDTO.getCode();
				rstDesc = resultDTO.getInfo();
			}
			else if(resultDTO != null && resultDTO.getData() != null) {
				@SuppressWarnings("unchecked")
				Map<String, String> ckResMap = (Map<String, String>) resultDTO.getData();
				chkRstCd = ckResMap.get("chkRstCd");
				rstDesc = ckResMap.get("rstDesc");
			}

			if(chkRstCd == null || Integer.valueOf(chkRstCd) != BaseConstant.IS_YESNO_YES) {
				result.setSuccess(BaseConstant.IS_YESNO_NO);
				result.setInfo("四要素信息有误，请修改");
//				if(StringUtils.isNotEmpty(rstDesc)) {
//					result.setSuccess(BaseConstant.IS_YESNO_NO);
//					result.setInfo(rstDesc);
//				}
//				else {
//					result.setSuccess(BaseConstant.IS_YESNO_NO);
//					result.setInfo(resultDTO.getInfo());
//				}
			}
		}
		catch (BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
			BaseLogUtils.error(logger, "save", "浙银金租立项保存失败 " + ex.getMessage());
			ex.printStackTrace();
		}
		catch (Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo("保存失败!");
			BaseLogUtils.error(logger, "save", "浙银金租立项保存失败 " + ex.getMessage());
			ex.printStackTrace();
		}

		return result;
	}

	private PrjCstAccInfoQueryCondition getRequestParamForPrjCstAccInfo(HttpServletRequest request, PrjCstAccInfoVO prjCstAccInfoVO) {
		PrjCstAccInfoQueryCondition prjCstAccInfoQC = new PrjCstAccInfoQueryCondition();

		//获取参数
		Long id = prjCstAccInfoVO == null ? null : prjCstAccInfoVO.getId();
		String prjId = request.getParameter("prjId"); // 项目ID
		String cstId = request.getParameter("cstId"); // 客户ID
		String cstNm = request.getParameter("cstNm");//客户名称
		String mp = request.getParameter("mp");//客户手机号
		String accNo = request.getParameter("accNo");//银行账号
		String certTypCd = DictionaryConstant.CODE_CERT_TYP_CD_0;//证件类型（CST0005）
		String bkNm = request.getParameter("bkNm");//开户银行名称
		String bkCd = request.getParameter("bkCd");//开户行号
		String bkNo = request.getParameter("bkNo");//开户行号
		String fundRasInsId = request.getParameter("fundRasInsId"); // 金融机构ID

		prjCstAccInfoQC.setPrjId(Long.valueOf(prjId));
		prjCstAccInfoQC.setCstId(Long.valueOf(cstId));
		prjCstAccInfoQC.setCstNm(cstNm);//客户名称
 		prjCstAccInfoQC.setMp(mp);//客户手机号
 		prjCstAccInfoQC.setAccNo(accNo);//银行账号
 		prjCstAccInfoQC.setCertTypCd(certTypCd);//证件类型（CST0005）
 		prjCstAccInfoQC.setBkNm(bkNm);//开户银行名称
 		prjCstAccInfoQC.setBkNo(bkNo);//开户行号
		prjCstAccInfoQC.setBkCd(bkCd);//开户银行代码
		prjCstAccInfoQC.setAccNm(cstNm);
 		if(fundRasInsId != null) {
 			prjCstAccInfoQC.setInsId(Long.valueOf(fundRasInsId));
 		}

		if (id != null) {
			prjCstAccInfoQC.setId(id);
		}

		return prjCstAccInfoQC;
	}
	
	private PrjCstAccInfoQueryCondition getRequestParamForCstNpBscInfo(HttpServletRequest request, CstNpBscInfoVO cstNpBscInfoVO) {
		String certValidSDt = request.getParameter("certValidSDt"); // 证件有效起始日期
		String certValidEDt = request.getParameter("certValidEDt"); // 证件有效终止日期
		String monIncmYuan = request.getParameter("monIncmYuan"); // 月收入
		cstNpBscInfoVO.setCertValidSDt(certValidSDt);
		cstNpBscInfoVO.setCertValidEDt(certValidEDt);
		if(StringUtils.isNotBlank(monIncmYuan)){
		cstNpBscInfoVO.setYrInc(DecimalUtils.convertYuanToFen(DecimalUtils.mul(Double.valueOf(monIncmYuan), 12D)));
		}
		return null;
	}
}
